<?

/*
  Template Name: Pagseguro
 */


if ($_GET) {
	$pagseguro = $_GET;
	foreach ($_GET as $key => $value) {
		$wpdb->query("insert into wp_log_pagseguro values(NULL, '$key', '$value', '_GET', NOW())");
	}
}

if ($_POST) {
	$pagseguro = $_POST;
	foreach ($_POST as $key => $value) {
		$wpdb->query("insert into wp_log_pagseguro values(NULL, '$key', '$value', '_GET', NOW())");
	}

	if ($_POST['Referencia'] && $_POST['StatusTransacao']) {
		$numero_cobranca = $_POST['Referencia'];
		$StatusTransacao = $_POST['StatusTransacao'];
		if ($StatusTransacao == 'Aprovado') {
			$wpdb->query("update wp_pedidos set status = '1' where referencia = '$numero_cobranca'");
			$cliente = $wpdb->get_results("select c.* from wp_cliente as c, wp_pedidos as p where p.cliente = c.id");
			$dados = $cliente[0];
			$body = mailPagamentoConfirmado($dados->nome, $numero_cobranca);
			$headers = "content-type: text/html; charset=UTF-8\r\n";
			wp_mail($dados->email, "Pagamento confirmado!", $body, $headers);
			wp_mail(get_settings('admin_email'), "Pagamento confirmado!", $body, $headers);
		}
	}
}
